package com.rotten;

import imageLoader.DrawableManager;

import java.util.Vector;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
	
	public static Vector<Movie> openings = new Vector<Movie>();
	Client cliente = new Client();
	public static boolean login = false;
	EditText nova_pesquisa;
	GridView grid_main;
	ProgressDialog dialog;   //barra de loading
	DrawableManager draw = new DrawableManager();
	Favorites fav = new Favorites();
	public static Vector<Movie> favoritos = new Vector<Movie>();
		
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      //full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);              
        
        setContentView(R.layout.main);        
               
        if(favoritos.size()==0) favoritos = fav.getFavorites();
                
       Button login_b = (Button) findViewById(R.id.login);
        if(login)
     	   login_b.setText("Log out");
        else 
        	login_b.setText("Twitter Login");
        
		login_b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(!login){  //se ainda nao tiver login feito vai para a pagina de login
					Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
					startActivity(myIntent);
				}
				//else termina sessao 
			}
		});
        
        Button go = (Button) findViewById(R.id.nova_pesquisa_main);
        nova_pesquisa = (EditText) findViewById(R.id.pesquisa_main);
        nova_pesquisa.setMaxLines(1);
        go.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {           	
            String pesq = nova_pesquisa.getText().toString();
           	 //Comeca uma nova actividade
           	 Intent i = new Intent(Main.this, Rotten.class);
           	 i.putExtra("pesquisa", pesq); 
           	 i.putExtra("pagina", 1);
             startActivity(i);	            	 
            }
		});
                    
        openings = cliente.parse_openigResults(cliente.showOpenings());
		// Coloca as estreias na grelha principal
		grid_main = (GridView) findViewById(R.id.GridView);
		grid_main.setAdapter( new ImageAdapter(this));

	}
	
	
	//Menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }	
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.home:  {
            	 break;
            }
            case R.id.refresh:  {
            	Intent i = new Intent(Main.this, Main.class);
            	startActivity(i);
           	 break;
           }            
            case R.id.sair:{
            	System.exit(0);
            	break;
            }
            case R.id.favoritos:{
            	Intent i = new Intent(Main.this, List_favorites.class);
                startActivity(i);	 
            	break;
            }
        }
        return true;
    }
    
	//Classe gerada para inserir as estreias na grelha pricipal
	public class ImageAdapter extends BaseAdapter{
		Context mContext;
			
		public static final int ACTIVITY_CREATE = 10;
		public ImageAdapter(Context c){
			mContext = c;
		}
		public int getCount() {
			return Main.openings.size();             //numero de filmes que aparece na pagina principal
		}
				
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v;
			if(convertView==null){
				Movie mov = new Movie();
				mov = Main.openings.get(position);
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.aux_capa_movie, null);
				TextView tv = (TextView)v.findViewById(R.id.movieNewName);
				tv.setText(mov.getTitle());   //alterar
				ImageView iv = (ImageView)v.findViewById(R.id.movieNew);				
				draw.fetchDrawableOnThread(mov.getPoster_thumbnail(), iv);
				iv.setId(mov.getId());
				int mov_selected = iv.getId();
			  	final int m2 = mov_selected;
				iv.setOnClickListener(new View.OnClickListener() {
		             public void onClick(View v) {
		                 // Perform action on click	 	            	
		            	//Toast.makeText(getApplicationContext(), m2 + "", Toast.LENGTH_LONG).show();
		            	 //Comeca uma nova actividade
		            	 Intent i = new Intent(Main.this, Movieprofile.class);
		            	 i.putExtra("movie", m2); //passa o filme como argumento
		                 startActivity(i);
		             }
				});
			}
			else
			{
				v = convertView;
			}
			return v;
		}
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
}